package bera.lee.memorycard;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 *<pre>
 * <b><font color="blue">MainMenu</font></b>
 * </pre>
 * 
 *<pre>
 * <b>&nbsp;--描述说明--</b>
 * </pre>
 * 
 * <pre></pre>
 * 
 * <pre>
 * <b>--样例--</b>
 *   MainMenu obj = new MainMenu();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * @author <b>beralee</b>
 */
public class MainMenu extends Activity {
	private ImageView b_points;

	private ImageView b_challenge;

	private ImageView b_setting;

	private ImageView b_exit;

	private ImageView b_score;

	private DbAdapter mDbHelper;

	private DisplayMetrics dm;

	public static MediaPlayer music_song;

	public static MediaPlayer music_win;

	public static boolean alive = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (!alive) {
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menulayout);
		mDbHelper = new DbAdapter(this);
		mDbHelper.open();
		this.initData();
		b_points = (ImageView) findViewById(R.id.guoguan);
		b_points.setClickable(true);
		b_challenge = (ImageView) findViewById(R.id.tianzhan);
		b_challenge.setClickable(true);
		b_setting = (ImageView) findViewById(R.id.setting);
		b_setting.setClickable(true);
		b_exit = (ImageView) findViewById(R.id.exit);
		b_exit.setClickable(true);
		b_score = (ImageView) findViewById(R.id.score);
		b_score.setClickable(true);
		b_points.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					b_points.setImageResource(R.drawable.guandown);
					break;
				case MotionEvent.ACTION_UP:
					b_points.setImageResource(R.drawable.guanup);
					Config.MODE = 0;
					Config.Point = 1;
					MapUtil.configByPoint();
					Config.DCARD = R.drawable.icon;
					Intent intent = new Intent(MainMenu.this, ChoosePoint.class);
					startActivity(intent);
					break;
				}
				return false;
			}
		});
		b_challenge.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					b_challenge.setImageResource(R.drawable.tiaozhandown);
					break;
				case MotionEvent.ACTION_UP:
					b_challenge.setImageResource(R.drawable.tiaozhanup);
					Config.MODE = 1;
					Config.Point = 0;
					Config.CARD_NUM = 48;
					Config.CARD_TYPE = "tiaozhan";
					Config.BACKGROUND = R.drawable.bg7;
					Intent intent = new Intent();
					intent.setClass(MainMenu.this, BitmapTouch.class);
					startActivity(intent);
					break;
				}
				return false;
			}
		});
		b_setting.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					b_setting.setImageResource(R.drawable.settingdown);
					break;
				case MotionEvent.ACTION_UP:
					b_setting.setImageResource(R.drawable.settingup);
					Intent intent = new Intent();
					intent.setClass(MainMenu.this, Setting.class);
					startActivity(intent);
					break;
				}
				return false;
			}
		});
		b_exit.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					b_exit.setImageResource(R.drawable.exitdown);
					break;
				case MotionEvent.ACTION_UP:
					// b_exit.setImageResource(R.drawable.exitup);
					alive = false;
					android.os.Process.killProcess(android.os.Process.myPid());
					break;
				}
				return false;
			}
		});
		b_score.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					b_score.setImageResource(R.drawable.scoredown);
					break;
				case MotionEvent.ACTION_UP:
					b_score.setImageResource(R.drawable.scoreup);
					Intent intent = new Intent();
					intent.setClass(MainMenu.this, HighScoreList.class);
					startActivity(intent);
					break;
				}
				return false;
			}
		});

	}

	@Override
	protected void onPause() {
		super.onPause();
		if (Config.OPEN_MUSIC) {
			music_song.release();
		}

	}

	private void initData() {
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int pwidth = dm.widthPixels;
		int pheight = dm.heightPixels;
		if(pwidth>350&&pwidth<500){
			if(pheight>680){
				Config.CARD_SIZE=64;
			}
		}
		music_song = MediaPlayer.create(this, R.raw.music1);
		music_song.setLooping(true);
		music_win = MediaPlayer.create(this, R.raw.win);
		Cursor cr = mDbHelper.fetchValue("openmusic");
		// 音乐是否打开
		String openmusic = cr.getString(cr.getColumnIndexOrThrow("value"));
		if (openmusic.equals("1")) {
			Config.OPEN_MUSIC = true;
			// music_song.start();
		}
		cr = mDbHelper.fetchValue("openbgm");
		String openbgm = cr.getString(cr.getColumnIndexOrThrow("value"));
		if (openbgm.equals("1")) {
			Config.OPEN_BGM = true;
		}
		cr = mDbHelper.fetchValue("maxpoint");
		String maxpoint = cr.getString(cr.getColumnIndexOrThrow("value"));
		Config.CAN_CHOOSE_Point = Integer.valueOf(maxpoint);
		cr = mDbHelper.fetchValue("temp1");
		String temp1 = cr.getString(cr.getColumnIndexOrThrow("value"));
		if(temp1.equals("")){
			//旧版，新增记录
			mDbHelper.add4Level();
			mDbHelper.updateValue("temp1", "1");
		}
	}

}
